/*
 * PanelEnemy.java
 *
 * Created on January 2, 2008, 8:08 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.UI;

import com.usoog.tdcore.creep.FactoryCreep;
import com.usoog.tdcore.gamegrid.ScaledCanvas;
import hextd.Constants;
import hextd.HexTD;
import hextd.core.GameManagerImplementation;
import hextd.creep.HexTDCreep;
import hextd.creep.HexTDCreepData;
import hextd.util.Cache;
import hextd.wave.PathEmpty;
import hextd.wave.Wave;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Point2D.Double;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author  hylke
 */
public class PanelEnemy extends javax.swing.JPanel implements ScaledCanvas {

	private List<HexTDCreep> enemies = new ArrayList<HexTDCreep>();
	private GameManagerImplementation gameManager;
	private HexTD game;
	private PathEmpty path = new PathEmpty();
	private int pHeight = 0;
	private int pWidth = 0;
	private double scale = 0;
	private int gameTime = 0;

	/** Creates new form PanelEnemy */
	public PanelEnemy() {
	}

	public void setContext(GameManagerImplementation gm, HexTD game) {
		gameManager = gm;
		this.game = game;
		initComponents();
	}

	public synchronized void clearEnemies() {
		enemies.clear();
		path.setNumCreeps(0);
	}

	public synchronized void addEnemy(Wave.EnemyInstance e) {
		FactoryCreep<HexTDCreep, HexTDCreepData> creepFactory = gameManager.getCreepFactory();
		HexTDCreepData data = new HexTDCreepData();
		data.health = e.health;
		data.reward = e.reward;
		data.size = e.size;
		data.path = path;
		HexTDCreep creep = creepFactory.getObject(e.type.getName(), data);
		enemies.add(creep);
		recalculateSize();
	}

	public synchronized void recalculateSize() {
		pWidth = getWidth();
		Cache.getInstance(this).clearCache();
		if (this.enemies.size() > 0) {
			pHeight = Math.min(pWidth / enemies.size(), getHeight());
			scale = pHeight;

			setPaintScale(scale);
			setDefaultStrokeWidth((float) scale / 40);
			for (Iterator<HexTDCreep> i = this.enemies.iterator(); i.hasNext();) {
				HexTDCreep e = i.next();
				e.resetScale(this);
			}
		}
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if (this.enemies.size() > 0) {
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g2.setColor(Color.BLACK);
			g2.fillRect(0, 0, this.pWidth, this.pHeight);
			try {
				for (Iterator<HexTDCreep> i = this.enemies.iterator(); i.hasNext();) {
					HexTDCreep e = i.next();
					e.paint(g2, this.gameTime);
				}
			} catch (ConcurrentModificationException e) {
			}
		} else {
			g2.setColor(Color.BLACK);
			g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		}
	}

	private void mouseOver(int x) {
		int nr = (int) (x / this.scale);
		if (nr < this.enemies.size()) {
			HexTDCreep e = this.enemies.get(nr);
			game.setInfoText(e.getInfoString(), false);
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(Constants.colorBackGround);
        setBorder(null);
        setForeground(Constants.colorForeGround);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
		this.mouseOver(evt.getX());
    }//GEN-LAST:event_formMouseMoved

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
		this.recalculateSize();
    }//GEN-LAST:event_formComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

	public void doTick(int gameTime) {
		this.gameTime = gameTime;
		try {
			for (Iterator<HexTDCreep> i = enemies.iterator(); i.hasNext();) {
				HexTDCreep e = i.next();
				e.doTick(gameTime);
			}
		} catch (ConcurrentModificationException e) {
		}
		this.repaint();
	}

	public double getGridPaintWidth() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void setGridPaintWidth(double gridPaintWidth) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public double getPaintScale() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void setPaintScale(double scale) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public float getDefaultStrokeWidth() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void setDefaultStrokeWidth(float size) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public double getOffsetX() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void setOffsetX(double offsetX) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public double getOffsetY() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void setOffsetY(double offsetY) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public Double fromGameToPaintCoordinates(Double gamePoint, Double target) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public Double fromPaintToGameCoordinates(Double paintPoint, Double target) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
